package indi.zhifa.recipe.bailan.busy.auth.controller.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import indi.zhifa.recipe.bailan.busy.auth.entity.dto.device.DeviceCreateDto;
import indi.zhifa.recipe.bailan.busy.auth.entity.dto.device.DeviceEditDto;
import indi.zhifa.recipe.bailan.busy.auth.entity.po.DeviceEntity;
import indi.zhifa.recipe.bailan.busy.auth.service.IDeviceService;
import indi.zhifa.recipe.bailan.framework.auth.entity.annotations.auth.DataPermission;
import indi.zhifa.recipe.bailan.framework.auth.entity.annotations.auth.RequireRole;
import indi.zhifa.recipe.bailan.framework.auth.entity.enums.EDataPermissionType;
import indi.zhifa.recipe.bailan.framework.auth.impl.util.AppConst;
import indi.zhifa.recipe.bailan.framework.common.entity.annotations.ZfRestController;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "DeviceApi-设备接口")
@RequestMapping("/api/device")
@Slf4j
@ZfRestController
@RequiredArgsConstructor
public class DeviceApi {

    private final IDeviceService mDeviceService;

    @Operation(summary = "查看设备信息")
    @GetMapping("/{id}}")
    DeviceEntity info(@Parameter(description = "id") @PathVariable(name = "id") Long pId){
        return mDeviceService.info(pId);
    }

    @RequireRole(roles = "company")
    @Operation(summary = "创建设备信息")
    @PostMapping
    DeviceEntity create(@Parameter(description = "设备配置") @RequestBody DeviceCreateDto pDeviceCreateDto){
        return mDeviceService.create(pDeviceCreateDto);
    }

    @RequireRole(roles = "company")
    @DataPermission(permissionType = EDataPermissionType.ID_EQ,domain = AppConst.DOMAIN_ZF_MERCHANT)
    @Operation(summary = "修改设备信息")
    @PutMapping("/{id}")
    DeviceEntity edit(@Parameter(description = "id") @PathVariable(name = "id") Long pId,
                      @Parameter(description = "设备配置") @RequestBody DeviceEditDto pDeviceEditDto){
        return mDeviceService.edit(pId,pDeviceEditDto);
    }


    @RequireRole(roles = "staff")
    @DataPermission(domain = "zf-mgr",permissionType = EDataPermissionType.AUTH_CASCADE)
    @Operation(summary = "查看分页-管理部门")
    @GetMapping("/page/deviceGroup")
    Page<DeviceEntity> pageByDeviceGroup(@Parameter(description = "当前页",required = true) @RequestParam(name = "current") int pCurrent,
                            @Parameter(description = "页大小",required = true) @RequestParam(name = "size") int pSize,
                            @Parameter(description = "设备码") @RequestParam(name = "code",required = false) String pCode,
                            @Parameter(description = "设备名字") @RequestParam(name = "name",required = false) String pName,
                            @Parameter(description = "创建者Id") @RequestParam(name = "createUserId",required = false) Long pUserId,
                            @Parameter(description = "公司Id") @RequestParam(name = "companyAuthId",required = false) Long pCompanyAuthId){
        return mDeviceService.page(pCurrent,pSize,pCode,pName,pUserId,pCompanyAuthId);
    }

    @RequireRole(roles = "company")
    @DataPermission(domain = "zf-merchant",permissionType = EDataPermissionType.AUTH_ID)
    @Operation(summary = "查看分页-公司")
    @GetMapping("/page/company")
    Page<DeviceEntity> pageByCompany(@Parameter(description = "当前页",required = true) @RequestParam(name = "current") int pCurrent,
                                         @Parameter(description = "页大小",required = true) @RequestParam(name = "size") int pSize,
                                         @Parameter(description = "设备码") @RequestParam(name = "code",required = false) String pCode,
                                         @Parameter(description = "设备名字") @RequestParam(name = "name",required = false) String pName,
                                         @Parameter(description = "创建者Id") @RequestParam(name = "createUserId",required = false) Long pUserId,
                                         @Parameter(description = "公司Id") @RequestParam(name = "companyAuthId",required = false) Long pCompanyAuthId){
        return mDeviceService.page(pCurrent,pSize,pCode,pName,pUserId,pCompanyAuthId);
    }

}
